"use strict";
const request =require('./request')
const Router = {
    async index( query ) {
        var _a;
        const { id } = query;
        const playListRes = await request(`http://m.music.migu.cn/migu/remoting/query_playlist_by_id_tag?onLine=1&queryChannel=0&createUserId=migu&contentCountMin=5&playListId=${id}`);
        const listInfo = (_a = playListRes === null || playListRes === void 0 ? void 0 : playListRes.rsp) === null || _a === void 0 ? void 0 : _a.playList[0];
        if (!listInfo) {
            return {
                result: 200,
                errMsg: playListRes.info || '服务异常',
            };
        }
        const { playListName: name, createName, createUserId, playCount, contentCount, image: picUrl, summary: desc, createTime, updateTime, tagLists, playListType } = listInfo;
        const baseInfo = {
            name,
            id,
            picUrl,
            playCount,
            trackCount: contentCount,
            desc,
            creator: {
                id: createUserId,
                name: createName || '',
            },
            createTime,
            updateTime,
            tagLists,
            list: [],
        };
        const cids = [];
        let pageNo = 1;
        while ((pageNo - 1) * 20 < contentCount) {
            const listPage = await request(`https://music.migu.cn/v3/music/playlist/${id}?page=${pageNo}`, );
            //console.log(`https://music.migu.cn/v3/music/playlist/${id}?page=${pageNo}`)
            let html = listPage;
            let str = html.substring(html.indexOf("歌单歌曲列表"),html.indexOf("/歌单歌曲列表"));
            while (str.indexOf("row J_CopySong") !== -1){
                str = str.substring(str.indexOf("row J_CopySong") + 5)
                let e = str.indexOf(">")
                // //console.log(str)
                let whole = str.substring(0,e);
                let value = whole.substring(whole.indexOf("data-cid=\"")+10)
                let end = value.indexOf("\"");
                let cid = value.substring(0,end);
                cids.push(cid)
            }
            pageNo += 1;
        }

        // const { contentList = []} = await request.send(`http://m.music.migu.cn/migu/remoting/playlistcontents_query_tag?playListType=${playListType}&playListId=${id}&contentCount=${contentCount}`)
        let songs = await request(`https://music.migu.cn/v3/api/music/audioPlayer/songs?type=1&copyrightId=${cids.join(',')}`)
        baseInfo.list = (songs.items || []).map(({ copyrightId, length = '00:00:00', songName, singers = [], albums = [], mvList = [], songId }) => ({
            id: songId,
            cid: copyrightId,
            name: songName,
            artists: singers.map(({ artistId, artistName }) => ({ id: artistId, name: artistName })),
            album: albums[0] ? { id: albums[0].albumId, name: albums[0].albumId } : undefined,
            duration: (length || '00:00:00').split(':').reduce((t, v, i) => t + Number(v) * Math.pow(60, 2 - i), 0),
            mvId: mvList[0] ? mvList[0].mvId : undefined,
            mvCid: mvList[0] ? mvList[0].copyrightId : undefined,
        }));
        //console.log(baseInfo.list)
        return {
            result: 100,
            data: baseInfo,
        };
        // for (let i in res.contentList) {
        //   const { songId } = contentList[i];
        //   const s = await require('./song')['/']({ req: { query: { id: songId }}}, request).catch(() => null);
        //   //console.log(s);
        //   s && baseInfo.list.push(s);
        // }
        // Promise.all(contentList.map(({ songId }) => (
        //
        // ))).then((resArr) => {
        //   baseInfo.list = resArr.map(({ data }) => data).filter((o) => Boolean(o));
        //   return res.send({
        //     result: 100,
        //     data: baseInfo,
        //   })
        // })
    },
};
exports.default = Router;
